var baseHttpUrl = "http://192.168.1.3:8081/";
var LoginUrl = baseHttpUrl + "api/login"; //登陆接口
var Rettoken = baseHttpUrl + "api/resetToken"; //重新获取token
var ConfigInit = baseHttpUrl + "api/config/init"; //配置信息
var GetKjDetail = baseHttpUrl + "api/machine/info"; //获取矿机详情
var MyMachine = baseHttpUrl + "api/machine/myMachine"; //我的矿机
var FinishMachine = baseHttpUrl + "api/machine/myFinishList"; //停止中的矿机
var RunningMachines = baseHttpUrl + "/api/machine/myRunningList"; //运行中的矿机
var GetMachineList = baseHttpUrl + "api/machine/list"; //获取矿机列表
var BuyMachine = baseHttpUrl + "api/machine/buy"; //购买矿机
var GetProfit = baseHttpUrl + "api/machine/getProfit"; //领取收益
var GetMemberInfo = baseHttpUrl + "api/member/info"; //获取会员信息
var ModifyUserInfo = baseHttpUrl + "api/member/update"; //修改用户信息
var ModifyAlipay = baseHttpUrl + "api/member/setPay"; //修改支付信息
var UploadSfZm = baseHttpUrl + "api/member/upload/front"; //上传身份证正面
var UploadSfFm = baseHttpUrl + "api/member/upload/back"; //上传身份证反面
var UploadNameCard = baseHttpUrl + "api/member/auth"; //实名认证
var UploadHead = baseHttpUrl + "api/member/upload/head"; //头像
var SendRigistCode = baseHttpUrl + "api/send"; //发送注册验证码
var MemberRegist = baseHttpUrl + "api/register"; //用户注册
var ForgetPs = baseHttpUrl + "api/forgotPwd"; //忘记密码
var SendBuyOrder = baseHttpUrl + "api/order/buy"; //发布买单
var GetOrders = baseHttpUrl + "api/order/list"; //获取挂单列表
var GetGuaDanDetail = baseHttpUrl + "api/order/detail"; //获取挂单详情
var SellOut = baseHttpUrl + "api/order/sell"; //挂单卖出
var GetTransingOrders = baseHttpUrl + "api/order/tradingList"; //获取交易中订单列表
var OrderCount = baseHttpUrl + "api/order/orderCount"; //订单统计
var GetTransingOrderDetail = baseHttpUrl + "api/order/transDetail"; //获取订单详情
var MyBuyOrder = baseHttpUrl + "api/order/mylist"; //我的挂单列表
var MyFinishOrders = baseHttpUrl + "api/order/finishList"; //我的已完成订单
var MyCancelOrders = baseHttpUrl + "api/order/cancelList"; //我的取消订单
var MarketInfo = baseHttpUrl + "api/pricebook/marketInfo"; //市场信息
var BuyerPay = baseHttpUrl + "api/order/buyerPay"; //买家付款
var SellerConfirm = baseHttpUrl + "api/order/sellerConfirm"; //卖家确认
var CancelTrans = baseHttpUrl + "api/order/cancelTrans"; //取消交易
var CancelBuy = baseHttpUrl + "api/order/cancelBuy"; //撤销买单
var UploadTransProof = baseHttpUrl + "api/order/upload/proof"; //上传交易凭证
var TeamInfo = baseHttpUrl + "api/team/info"; //团队信息
var TeamOneList = baseHttpUrl + "api/team/oneList"; //一级
var TeamTwoList = baseHttpUrl + "api/team/twoList"; //二级
var TeamThreeList = baseHttpUrl + "api/team/threeList"; //三级
var AccountingBuyList = baseHttpUrl + "api/accounting/buyList"; //买入
var AccountingsellList = baseHttpUrl + "api/accounting/sellList"; //卖出
var AccountingProfitList = baseHttpUrl + "api/accounting/profitList"; //收益
var AccountingRewardList = baseHttpUrl + "api/accounting/rewardList"; //奖励
var GetNotices = baseHttpUrl + "api/service/notice"; //获取公告列表
var GetNoticeDetail = baseHttpUrl + "api/service/noticeDetail"; //获取公告详情
var GetNewsList = baseHttpUrl + "api/service/news"; //获取新闻列表
var GetNewsDetail = baseHttpUrl + "api/service/newsDetail"; //获取新闻详情
var PriceChart = baseHttpUrl + "api/pricebook/priceChart"; //获取公告列表
var GetFaqList = baseHttpUrl + "api/service/faq"; //获取新手问答列表
var GetFaqDetail = baseHttpUrl + "api/service/faqDetail"; //获取新手问答详情
var CheckUpdate = baseHttpUrl + "api/config/checkUpdate"; //检查更新

//mui的toast
function mymui(msg) {
	mui.toast(msg, {
		duration: 'short',
		type: 'richtext' //解决有的手机前面显示应用名称的问题
	})
}

//显示原生的toast
function myNativeToast(msg) {
	plus.nativeUI.toast(msg);
}

var mToken = "";

function getTooken() {
	if(plus.storage.getItem('tooken') !== '') {
		return plus.storage.getItem('tooken');
	}
	return "";
}

var mConsole = {};
mConsole.log = function(msg) {
	var bool = true;
	if(bool) {
		console.log(msg);
	}
}

function copyArr(arr) {
	var res = [];
	for(var i = 0; i < arr.length; i++) {
		res.push(arr[i]);
	};
	return res;
}

mui.postJSON = function(url, data, callback) {
	mui.ajax({
		url: url,
		type: 'POST',
		timeout: 30000,
		data: data ? data : null,
		dataType: "json",
		headers: {
			'Content-Type': 'application/json',
			'charset': 'utf-8',
			'token': plus.storage.getItem("tooken")
		},
		success: function(result) {
			if(callback && typeof callback === "function") {
				callback(result);
			}
			if(result.code == 999) {
				if(!plus.webview.currentWebview().getURL().endWith("index.html")) {
					var index = plus.webview.getLaunchWebview();
					index.show();
					var wvs = plus.webview.all();
					for(var i = 0; i < wvs.length; i++) {
						if(wvs[i].id == index.id) {
							continue;
						}
						wvs[i].hide();
						wvs[i].close();
					}
				}
			}
		},
		error: function(xhr, type, errorThrown) {
			mymui("网络异常：" + type);
		}
	});
}

mui.postParam = function(url, data, callback) {
	mui.ajax({
		url: url,
		type: 'POST',
		timeout: 30000,
		data: data ? data : null,
		dataType: "json",
		headers: {
			'charset': 'utf-8',
			'token': plus.storage.getItem("tooken")
		},
		success: function(result) {
			if(callback && typeof callback === "function") {
				callback(result);
			}
			if(result.code == 999) {
				if(!plus.webview.currentWebview().getURL().endWith("index.html")) {
					var index = plus.webview.getLaunchWebview();
					index.show();
					var wvs = plus.webview.all();
					for(var i = 0; i < wvs.length; i++) {
						if(wvs[i].id == index.id) {
							continue;
						}
						wvs[i].hide();
						wvs[i].close();
					}
				}
			}
		},
		error: function(xhr, type, errorThrown) {
			mymui("网络异常：" + type);
		}
	});
}

var util = function() {
	var toStr = function(str) {
		if(str) {
			return String(str);
		} else {
			console.error(str + "不是String类型");
			return "";
		}
	};

	return {
		toStr: toStr
	};
}();

//右滑关闭 需要放入mui.plusReady里
function swipeBack() {
	//获取当前窗口,下面代码按界面任意地方都可右滑关闭
	var ws = plus.webview.currentWebview();
	//监听当前侧滑窗口的右滑
	ws.drag({
		direction: 'right',
		moveMode: 'followFinger'
	}, {
		view: plus.runtime.appid,
		moveMode: 'silent'
	}, function(e) {
		//滑动到end执行mui.back()事件
		if(e.type == 'end' && e.result) {
			mui.back();
		}
	});
}

String.prototype.startWith = function(str) {
	var reg = new RegExp("^" + str);
	return reg.test(this);
}

String.prototype.endWith = function(str) {
	var reg = new RegExp(str + "$");
	return reg.test(this);
}